package com.yhsoft.goods.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yhsoft.common.response.PageResult;
import com.yhsoft.common.service.TokenService;
import com.yhsoft.common.vo.Result;
import com.yhsoft.goods.controller.requset.AddGoodsInVo;
import com.yhsoft.goods.controller.requset.DeleteInVo;
import com.yhsoft.goods.controller.requset.SubmitInVo;
import com.yhsoft.goods.controller.requset.VenderGoodsListInVo;
import com.yhsoft.goods.controller.response.AllGoodsOutVo;
import com.yhsoft.goods.controller.response.CompanyNameOutVo;
import com.yhsoft.goods.entity.GoodsInfo;
import com.yhsoft.goods.service.ICompanyGoodsService;
import com.yhsoft.goods.service.IGoodsInfoService;
import com.yhsoft.sys.entity.CompanyInfo;
import com.yhsoft.sys.service.ICompanyInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author: yyyyyyyh
 * @create: 2023-11-16
 * @Version 1.0
 **/
@Api(tags = {"J-供货商商品管理"})
@RestController
@RequestMapping("/venderGoods")
public class VenderGoodsController {

    @Resource
    private ICompanyInfoService companyInfoService;

    @Resource
    private ICompanyGoodsService iCompanyGoodsService;

    @Resource
    private IGoodsInfoService iGoodsInfoService;

    @ApiOperation("获取当前用户公司名称")
    @GetMapping("/getCompanyName")
    public Result<CompanyNameOutVo> getCompanyName() {
        Integer companyId = TokenService.getUserBytoken().getCompanyId();
        CompanyInfo companyInfo = companyInfoService.getById(companyId);
        CompanyNameOutVo outVo = new CompanyNameOutVo();
        outVo.setCompanyId(companyId);
        outVo.setCompanyName(companyInfo.getCompanyName());
        return Result.success(outVo);
    }

    @ApiOperation("添加商品")
    @PostMapping("/addGoods")
    public Result<?> addGoods(@Validated @RequestBody AddGoodsInVo inVo) {
        if (inVo.getId() == null) {
            if (iGoodsInfoService.exists(inVo.getGoodsName())) {
                return Result.success("不能重复新增！");
            }
            iGoodsInfoService.addGoods(inVo);
        } else {
            iGoodsInfoService.updateGoods(inVo);
        }

        return Result.success("保存成功！");
    }

    @ApiOperation("商品管理")
    @GetMapping("/list")
    public Result<PageResult<AllGoodsOutVo>> getVenderGoodsList(VenderGoodsListInVo inVo,
                                                                @RequestParam(value = "pageNo") Long pageNo,
                                                                @RequestParam(value = "pageSize") Long pageSize) {
        IPage<AllGoodsOutVo> iPage = iGoodsInfoService.getVenderGoodsList(inVo, pageNo, pageSize);
        return Result.success(PageResult.valueOf(iPage));
    }

    @ApiOperation("批量删除商品")
    @PostMapping("/delete")
    public Result<?> delete(@RequestBody DeleteInVo inVo) {
        List<Integer> goodsIds = inVo.getGoodsIds();
        if (goodsIds == null || goodsIds.isEmpty()) {
            return Result.fail("请选择商品！");
        }
        for (Integer id : goodsIds) {
            Integer statusVerify = iGoodsInfoService.getById(id).getStatusVerify();
            if (statusVerify == 1) {
                return Result.fail("商品(id:" + id + ")已通过审核，不能删除！");
            }
        }
        goodsIds.forEach(id -> {
            iGoodsInfoService.getBaseMapper().deleteById(id);
        });
        return Result.success("删除成功！");
    }

    @ApiOperation("提交审核")
    @PostMapping("/submit")
    public Result<?> submit(@RequestBody SubmitInVo inVo) {
        if (inVo.getGoodsId() == null) {
            return Result.fail("请选择商品！");
        }
        GoodsInfo goodsInfo = iGoodsInfoService.getById(inVo.getGoodsId());
        if(goodsInfo == null) {
            return Result.fail("获取商品失败！");
        }
        goodsInfo.setStatusVerify(0);
        iGoodsInfoService.updateById(goodsInfo);
        return Result.success("提交成功！");
    }


}
